import time
import requests
import json
import sys
 
# 配置你的Gitee账号的access token
access_token = '42f7e0d444e98c7a0ac4a93c2b7fbf75'


 
# 使用Gitee提供的API，例如获取用户的所有仓库
headers = {
    'Authorization': 'token ' + access_token
}
 
pr_lists = []
pr_to_delete_lists = []

date = sys.argv[1]
since_time = "%s-01" % date
until_time = "%s-31" % date

print("begin to get PR id")
# since统计
currpage = 1
# 已合并
while True:
    api_url_pr_1 = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls?state=merged&sort=created&since=%s&page=%s&per_page=20" % (since_time, currpage)
    response = requests.get(api_url_pr_1, headers=headers)
    if response.status_code == 200:
        repositories = response.json()
        if not repositories:
            break
        for res in repositories:
            onelist = res['html_url']
            pr_num = onelist.split('/')[-1]
            pr_lists.append(pr_num)
        currpage += 1
    else:
        print('Error:', response.status_code)

currpage = 1
# 已关闭
while True:
    api_url_pr_2 = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls?state=closed&sort=created&since=%s&page=%s&per_page=20" % (since_time, currpage)
    response = requests.get(api_url_pr_2, headers=headers)
    if response.status_code == 200:
        repositories = response.json()
        if not repositories:
            break
        for res in repositories:
            onelist = res['html_url']
            pr_num = onelist.split('/')[-1]
            pr_lists.append(pr_num)
        currpage += 1
    else:
        print('Error:', response.status_code)


# until统计
currpage = 1
# 已合并
while True:
    api_url_pr_1 = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls?state=merged&sort=created&since=%s&page=%s&per_page=20" % (until_time, currpage)
    response = requests.get(api_url_pr_1, headers=headers)
    if response.status_code == 200:
        repositories = response.json()
        if not repositories:
            break
        for res in repositories:
            onelist = res['html_url']
            pr_num = onelist.split('/')[-1]
            pr_to_delete_lists.append(pr_num)
        currpage += 1
    else:
        print('Error:', response.status_code)

currpage = 1
# 已关闭
while True:
    api_url_pr_2 = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls?state=closed&sort=created&since=%s&page=%s&per_page=20" % (until_time, currpage)
    response = requests.get(api_url_pr_2, headers=headers)
    if response.status_code == 200:
        repositories = response.json()
        if not repositories:
            break
        for res in repositories:
            onelist = res['html_url']
            pr_num = onelist.split('/')[-1]
            pr_to_delete_lists.append(pr_num)
        currpage += 1
    else:
        print('Error:', response.status_code)

# 删除until的pr_list
pr_lists = list(set(pr_lists) - set(pr_to_delete_lists))
print("pr lists: %s" % pr_lists)

data_lists_comment = []
count_comment = {}
count_commented = {}
count_pr = {}

print("calculating PR data......")
for one_pr in pr_lists:
    currpage = 1

    api_url_pr_user = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls/%s" % one_pr
    response = requests.get(api_url_pr_user, headers=headers)
    if response.status_code == 200:
        res = response.json()
        comment_creator = res["user"]["name"]
        one_pr_commented = 0

    
    while True:
        api_url_comment = "https://gitee.com/api/v5/repos/openeuler/cantian/pulls/%s/comments?page=%s&per_page=20&comment_type=diff_comment" % (one_pr,currpage)
        response = requests.get(api_url_comment, headers=headers)
        if response.status_code == 200:
            repositories = response.json()
            if not repositories:
                break
            for res in repositories:
                name = res['user']['name']
                comment = res['body']
                time = res['created_at']
                data_lists_comment.append([name,comment,time,one_pr])
                # print([name,time,comment,one_pr])
                tmp_num = count_comment.get(name, 0)
                count_comment[name] = tmp_num + 1
                one_pr_commented += 1
            currpage += 1
        else:
            print('Error:', response.status_code)
    tmp_num = count_commented.get(comment_creator, 0)
    count_commented[comment_creator] = tmp_num + one_pr_commented
    tmp_num = count_pr.get(comment_creator, 0)
    count_pr[comment_creator] = tmp_num + 1

log_pr_list = []
for one in data_lists_comment:
    log_pr_list.append({"name": one[0], "comment": one[1], "time": one[2], "PR": one[3]})
with open("stat/%s/cantian_pr.json" % date, "w") as f:
    json.dump(log_pr_list, f, indent=4)

cantian_count_comment_list = []
for one in count_comment:
    cantian_count_comment_list.append({"name": one, "comment_count": count_comment[one]})
cantian_count_comment_list.sort(key = lambda x: x['comment_count'], reverse=True)
with open("stat/%s/cantian_count_comment.json" % date, "w") as f:
    json.dump(cantian_count_comment_list, f, indent=4)

cantian_count_commented_list = []
for one in count_commented:
    cantian_count_commented_list.append({"name": one, "commented_count": count_commented[one]})
cantian_count_commented_list.sort(key = lambda x: x['commented_count'], reverse=True)
with open("stat/%s/cantian_count_commented.json" % date, "w") as f:
    json.dump(cantian_count_commented_list, f, indent=4)

count_pr_list = []
for one in count_pr:
    count_pr_list.append({"name": one, "pr_count": count_pr[one]})
count_pr_list.sort(key = lambda x: x['pr_count'], reverse=True)
with open("stat/%s/cantian_count_pr.json" % date, "w") as f:
    json.dump(count_pr_list, f, indent=4)
